package com.shushan.exercise;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.BaseExerciseHeaderActivity;
import com.shushan.base.Constants;
import com.shushan.mixedexam.MixedExamActivity;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @ClassName: SmartExerciseActivity
 * @Desciption: //TODO
 * @author: yichaohua
 * @date: 2018-12-11
 */
public class SmartExerciseActivity extends BaseExerciseHeaderActivity implements View.OnClickListener {

    private static final String TAG = "SmartExerciseActivity";

    private TextView hintText;
    private Button startButton;

    private int status = 0;
    private BabyApplication shuShanApplication = BabyApplication.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.smart_exercise_activity);

        setTitle("智能练习");

        initView();

        setHeaderText(shuShanApplication.getCurrentChildBean());

        getData();
    }

    private void initView(){
        hintText = findViewById(R.id.smart_exercise_hint_tv);
        startButton = findViewById(R.id.smart_exercise_btn);
        startButton.setOnClickListener(this);
    }

    private void getData() {
        int childId = shuShanApplication.getCurrentChildId();
        showWaitDialog();

        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.GET_ZN_LIST);
        UrlParse paramParse = new UrlCommParse(this).putValue("child_id", childId)
                .putValue("practice_type",2);

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(String data) {
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            if(jsonObject != null){
                                status = jsonObject.optInt("status",0);
                                if(status == 0){
                                    hintText.setText("你的练习过少，没有足够数据可分析");
                                }else {
                                    hintText.setText("你有智能练习未完成");
                                }

                                JSONObject rewardJson = jsonObject.optJSONObject("reward");
                                int score = rewardJson.optInt("score",0);
                                int time = rewardJson.optInt("time",0);
                                setHeaderText(score,time);
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.smart_exercise_btn:
                if(status == 0){
                    showToast("你的练习过少，没有足够数据可分析");
                }else {
                    Intent intent = new Intent(this, MixedExamActivity.class);
                    intent.putExtra("examType", Constants.TYPE_SMART_EXERCISE);
                    startActivityAllow(intent);

                    onBackAction();
                }
                break;
            default:
                break;
        }
    }
}
